package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticelContentMapper;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.model.article.constant.ArticleConstants;
import com.heima.model.article.dots.ApArticleDto;
import com.heima.model.article.dots.ArticleHomeDto;
import com.heima.model.article.pojo.ApArticle;
import com.heima.model.article.pojo.ApArticleConfig;
import com.heima.model.article.pojo.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.nntp.Article;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper ;

    @Autowired
    private ApArticleConfigMapper configMapper ;

    @Autowired
    private ApArticelContentMapper contentMapper ;

    @Autowired
    private ArticleFreemarkerService freemarkerService ;


    @Override
    public ResponseResult load(ArticleHomeDto articleHomeDto, short operationType) {
        //参数校验
        //1 校验页面显示条数
        Integer size = articleHomeDto.getSize();
        if (size == null || size ==0){
            size = ArticleConstants.DEFAULT_PAGE_SIZE;
        }
        size = Math.min(size , ArticleConstants.MAX_PAGE_SIZE);
        articleHomeDto.setSize(size);
        //2 校验时间
        if (articleHomeDto.getMaxBehotTime() == null){
            articleHomeDto.setMaxBehotTime(new Date());
        }
        if (articleHomeDto.getMinBehotTime() == null){
            articleHomeDto.setMinBehotTime(new Date());
        }

        //3 频道校验
        if (articleHomeDto.getTag() == null){
            articleHomeDto.setTag(ArticleConstants.DEFAULT_TAG);
        }

        //4 操作类型校验
        if (operationType != ArticleConstants.OPERTATION_LOAD_MORE && operationType != ArticleConstants.OPERTATION_LOAD_NEW){
            operationType = ArticleConstants.OPERTATION_LOAD_MORE ;
        }

        List<ApArticle> apArticles = apArticleMapper.loadArticleList(articleHomeDto, operationType);
        log.info(apArticles.toString());
        return ResponseResult.okResult(apArticles);
    }

    //保存app端相关文章
    @Override
    public ResponseResult saveArticle(ApArticleDto dto) {

//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        //1检查参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticle article = new ApArticle();
        BeanUtils.copyProperties(dto , article);

        //2判断是否存在id
        //不存在 ，保存 文章 文章配置 文章内容
        if (dto.getId() == null ){
            //保存文章
            save(article);
            //保存配置
            ApArticleConfig apArticleConfig = new ApArticleConfig(article.getId());
            configMapper.insert(apArticleConfig);
            //保存文章内容
            ApArticleContent content = new ApArticleContent();
            content.setArticleId(article.getId());
            content.setContent(dto.getContent());
            contentMapper.insert(content);
        }else {
            //存在 修改 文章 文章内容
            //修改文章
            updateById(article);
            //先通过articleId查到content对象，重新给他设置内容
            ApArticleContent content = contentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, article.getId()));
            content.setContent(dto.getContent());
            //修改文章内容
             contentMapper.updateById(content);
        }

        //保存文章的时候，生成静态文件并上传到mio中
        freemarkerService.buildArticleToMinio(article ,dto.getContent());

        //3 结果返回，返回文章id
        return ResponseResult.okResult(article.getId());
    }
}
